package com.xxxx.server.service.impl;
import java.time.LocalDateTime;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxxx.server.mapper.MailLogMapper;
import com.xxxx.server.pojo.Employee;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.pojo.MailConstants;
import com.xxxx.server.pojo.MailLog;
import com.xxxx.server.pojo.Salary;
import com.xxxx.server.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.server.vo.PageBean;
import com.xxxx.server.vo.RespBean;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 学海无涯
 * @since 2021-11-15
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private MailLogMapper mailLogMapper;

    /**
     * 分页获取所有员工
     * @param currentPage
     * @param pageSize
     * @param employee
     * @param beginDataScope
     * @return
     */
    @Override
    public PageBean getAllEmployee(Integer currentPage, Integer pageSize, Employee employee, LocalDate[] beginDataScope) {
        // 开启分页
        Page<Employee> page = new Page<>(currentPage,pageSize);
        IPage<Employee> iPage = employeeMapper.getAllEmployee(page,employee,beginDataScope);
        PageBean pageBean = new PageBean(iPage.getTotal(),iPage.getRecords());
        return pageBean;
    }

    /**
     * 获取工号
     */
    @Override
    public RespBean<String> getMaxWorkID() {
        List<Map<String, Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workID)"));
        int maxWorkID = Integer.parseInt(maps.get(0).get("max(workID)").toString())+1;
        String maxWorkIDStr = String.format("%08d", maxWorkID);
        return new RespBean<String>().success("ok",maxWorkIDStr);
    }

    /**
     * 添加员工
     * @param employee
     */
    @Override
    public RespBean addEmp(Employee employee) {
        // 处理合同期限，保留两位小数
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365.00)));
        int insert = employeeMapper.insert(employee);
        if (1 == insert){
            // 发送信息
            Employee emp = employeeMapper.getEmployee(employee.getId()).get(0);
            // 数据库记录发送的消息
            String msgID = UUID.randomUUID().toString();
            MailLog mailLog = new MailLog();
            mailLog.setMsgId(msgID);
            mailLog.setEid(employee.getId());
            mailLog.setStatus(0);
            mailLog.setRouteKey(MailConstants.MAIL_ROUTING_KEY_NAME);
            mailLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailLog.setCount(0);
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT));
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());
            mailLogMapper.insert(mailLog);
            // 发送信息
            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME,MailConstants.MAIL_ROUTING_KEY_NAME,emp,new CorrelationData(msgID));

            return new RespBean().success("添加成功！");
        }
        return new RespBean().error("添加失败");
    }

    /**
     * 查看员工详情
     * @param id
     * @return
     */
    @Override
    public List<Employee> getEmployee(Integer id) {
        return employeeMapper.getEmployee(id);
    }

    /**
     * 获取所有员工的工资账套
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageBean getAllEmployeeWithSalary(Integer currentPage, Integer pageSize) {
        // 开启分页
        Page<Employee> page = new Page<>(currentPage,pageSize);
        IPage<Employee> iPage = employeeMapper.getALLEmployeeWithSalary(page);
        PageBean pageBean = new PageBean(iPage.getTotal(),iPage.getRecords());
        return pageBean;
    }
}
